package mianxiangduixiang.Test8;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Author JetZhang
 * Package:mianxiangduixiang.Test8
 * Description:编写一个应用程序，设计一个窗体，其中有4个按钮，分别命名为 “ 加”、“减”、“乘”、“除”，以及3个文本框，单击相应的按钮，将两个文本框的数字做算术运算，在第3个文本框中显示结果。要求处理NumberFormatException。
 * @Date 2021/11/18
 */

public class T1 {

    private static JTextField num1;
    private static JLabel cacu;
    private static JTextField num2;
    private static JTextField num3;

    public static void main(String[] args) {
        JFrame frame = new JFrame("算术运算");
        frame.setSize(350, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {

        panel.setLayout(null);

        num1 = new JTextField(20);
        num1.setBounds(10,20,40,25);
        panel.add(num1);

        cacu = new JLabel("+");
        cacu.setBounds(50,20,10,25);
        panel.add(cacu);
        num2 = new JTextField(20);
        num2.setBounds(60,20,40,25);
        panel.add(num2);

        JLabel eq = new JLabel("=");
        eq.setBounds(100,20,10,25);
        panel.add(eq);
        num3 = new JTextField(20);
        num3.setBounds(110,20,40,25);
        panel.add(num3);

        JButton add = new JButton("+");
        add.setBounds(10, 80, 80, 25);
        panel.add(add);

        cacus(add);

        JButton reduce = new JButton("-");
        reduce.setBounds(120, 80, 80, 25);
        panel.add(reduce);

        cacus(reduce);

        JButton multi = new JButton("*");
        multi.setBounds(10, 120, 80, 25);
        panel.add(multi);

        cacus(multi);


        JButton sub = new JButton("/");
        sub.setBounds(120, 120, 80, 25);
        panel.add(sub);

        cacus(sub);

    }

    private static void cacus(JButton btn) {
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (!num1.getText().isEmpty() && !num2.getText().isEmpty()){
                    cacu.setText(btn.getText());
                    Double num1Double = null;
                    Double num2Double = null;
                    try {
                        num1Double = Double.valueOf(num1.getText());
                        num2Double = Double.valueOf(num2.getText());
                    } catch (NumberFormatException ex) {
                        num1Double=0.0;
                        num2Double=0.0;
                        num1.setText(num1Double+"");
                        num2.setText(num2Double+"");
                    }
                    switch (btn.getText()){
                        case "+":
                            num3.setText((num1Double+num2Double)+"");
                            break;
                            case "-":
                            num3.setText((num1Double-num2Double)+"");
                            break;case "*":
                            num3.setText((num1Double*num2Double)+"");
                            break;case "/":
                            num3.setText((num1Double/num2Double)+"");
                            break;
                    }
                }else {

                }
            }
        });
    }
}
